home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 005 / text.demo / text.demo.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  11KB  |  325 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* text.demo.c */
  31.  
  32. /* sample program that asks AvailFonts() to make a list of the fonts
  33.  * that are available and makes a list of them, then opens a separate
  34.  * window and prints a description of the various attributes that can
  35.  * be applied to the fonts, in the font itself.  Notice that not all
  36.  * fonts accept all attributes (garnet9 for example, won't underline)
  37.  *
  38.  * Also note, if you run this, that not all fonts are as easily readable
  39.  * in the various bold and italicized modes.... this rendering is done
  40.  * in a fixed manner by software and the fonts were not necessarily
  41.  * designed to accept it.  It is always best to have a font that has
  42.  * been designed with a bold or italic characteristic built-in rather
  43.  * than try to bold-ize or italicize and existing plain font.
  44.  *
  45.  * Author: Rob Peck, 12/1/85
  46.  *
  47.  * This code may be freely utilized to create programs for the Amiga 
  48.  */
  49.  
  50.  
  51. #define AFTABLESIZE 2000
  52.  
  53. #include "exec/types.h"
  54. #include "exec/io.h"
  55. #include "exec/memory.h"
  56.  
  57. #include "graphics/gfx.h"
  58. #include "hardware/dmabits.h"
  59. #include "hardware/custom.h"
  60. #include "hardware/blit.h"
  61. #include "graphics/gfxmacros.h"
  62. #include "graphics/copper.h"
  63. #include "graphics/view.h"
  64. #include "graphics/gels.h"
  65. #include "graphics/regions.h"
  66. #include "graphics/clip.h"
  67. #include "exec/exec.h"
  68. #include "graphics/text.h"
  69. #include "graphics/gfxbase.h"
  70. #include "devices/keymap.h"
  71. #include "libraries/dos.h"
  72. #include "graphics/text.h"
  73. #include "libraries/diskfont.h"
  74. #include "intuition/intuition.h"
  75.  
  76. struct AvailFonts *af;
  77. struct AvailFontsHeader *afh;
  78. extern int AvailFonts();
  79.  
  80. struct TextFont *tf;
  81. struct TextAttr ta;
  82.  
  83. ULONG DosBase;
  84. ULONG DiskfontBase;
  85. ULONG IntuitionBase;
  86. ULONG GfxBase;
  87.  
  88. struct NewWindow nw = {
  89.         10, 10,        /* starting position (left,top) */
  90.         620,40,        /* width, height */
  91.         -1,-1,          /* detailpen, blockpen */
  92.         0,        /* flags for idcmp */
  93.   WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|SIMPLE_REFRESH|ACTIVATE|GIMMEZEROZERO,
  94.             /* window gadget flags */
  95.         0,              /* pointer to 1st user gadget */
  96.         NULL,           /* pointer to user check */
  97.         "Text Font Test", /* title */
  98.         NULL,           /* pointer to window screen */
  99.         NULL,           /* pointer to super bitmap */
  100.         100,45,         /* min width, height */
  101.         640,200,        /* max width, height */
  102.         WBENCHSCREEN};
  103.  
  104. struct Window *w;
  105. struct RastPort *rp;
  106.  
  107. SHORT text_styles[ ] = { FS_NORMAL, FSF_UNDERLINED, FSF_ITALIC, FSF_BOLD, 
  108.             FSF_ITALIC | FSF_BOLD, FSF_BOLD | FSF_UNDERLINED,
  109.             FSF_ITALIC | FSF_BOLD | FSF_UNDERLINED };
  110.         
  111. char *text[ ] = { " Normal Text", " Underlined", " Italicized", " Bold", 
  112.           " Bold Italics", " Bold Underlined", 
  113.           " Bold Italic Underlined" };
  114. char textlength[ ] = { 12, 11, 11, 5, 13, 16, 23 };
  115.  
  116. char *pointsize[] = { " 0"," 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9",
  117.                    "10","11","12","13","14","15","16","17","18","19",
  118.                    "20","21","22","23","24","25","26","27","28","29",
  119.                    "30","31"};
  120.  
  121. char fontname[40];
  122. char dummy[100];     /* provided for string length calculation */
  123. char outst[100];    /* build something to give to Text, see note in 
  124.              * the program body about algorithmically
  125.              * generated styles 
  126.              */
  127.  
  128. main()
  129. {
  130.     UBYTE fonttypes;
  131.     int j,k,m;
  132.     SHORT afsize;
  133.     SHORT style;
  134.     SHORT sEnd;    /* numerical position of end of string terminator,
  135.              * and coincidently the length of the string. */
  136.  
  137.     if( (DosBase = OpenLibrary("dos.library", 0)) == NULL) exit(-1);
  138.     if((DiskfontBase=OpenLibrary("diskfont.library",0))==NULL) exit(-4);
  139.     if((IntuitionBase=OpenLibrary("intuition.library",0))==NULL) exit(-2);
  140.     if((GfxBase=OpenLibrary("graphics.library",0))==NULL) exit(-3);
  141.     
  142.     tf=NULL;    /* no font currently selected */
  143.     afsize = AFTABLESIZE;    /* show how large a buffer is available */
  144.     fonttypes = 0xff;    /* show us all font types */
  145.     
  146.     afh = (struct AvailFontsHeader *) AllocMem(afsize, MEMF_CLEAR);
  147.     if(afh == NULL) exit(-5);
  148.     
  149.     printf("\nSearching for Fonts\n");
  150.     AvailFonts(afh, afsize, fonttypes);
  151.  
  152.     af = (struct AvailFonts *) &afh[1];  /* bypass header to get to the
  153.                           * first of the availfonts */
  154.  
  155.     for (j = 0; j < afh->afh_NumEntries; j++)
  156.         {
  157.     if((af->af_Attr.ta_Flags & FPF_REMOVED) ||
  158.        (af->af_Attr.ta_Flags & FPF_REVPATH) ||
  159.                     ((af->af_Type&AFF_MEMORY)&&
  160.             (af->af_Attr.ta_Flags&FPF_DISKFONT)))
  161.             ;    /* do nothing if font is removed, or if
  162.                  * font designed to be rendered rt->left
  163.                  * (simple example writes left to right)
  164.                  * or if font both on disk and in ram, 
  165.                  * don't list it twice. */
  166.  
  167.     /* AvailFonts performs an AddFont to the system list;
  168.      * if run twice, you get two entries, one of "af_Type 1" saying
  169.      * that the font is memory resident, and the other of "af_Type 2"
  170.      * saying the font is disk-based.  The third part of the 
  171.      * if-statement lets you tell them apart if you are scanning
  172.      * the list for unique elements;  it says "if its in memory and
  173.      * it is from disk, then don't list it because you'll find another
  174.      * entry in the table that says it is not in memory, but is on disk.
  175.      * (Another task might have been using the font as well, creating
  176.      * the same effect).
  177.      */
  178.  
  179.     else
  180.        {
  181.         printf("\nFont name found was: %ls",af->af_Attr.ta_Name);
  182.         printf("  and its point size is: %ld",af->af_Attr.ta_YSize);
  183.         /* Style parameter is in af->af_Attr.ta_Style,
  184.          * Flags parameter is in af->af_Attr.ta_Flags.
  185.          */        
  186.        }
  187.     af++;
  188.         }
  189.     /* now that we've listed the fonts, lets look at them */
  190.  
  191.     w = (struct Window *)OpenWindow(&nw);
  192.     rp = w->RPort;
  193.  
  194.     for(m=0; m<2; m++)    /* do normal video, then inverse video */
  195.      {
  196.  
  197.     af = (struct AvailFonts *)&afh[1]; /* reset value of af to original */
  198.     SetAPen(rp,1);        
  199.  
  200.     if(m == 0)SetDrMd(rp,JAM1);
  201.     else SetDrMd(rp,JAM1+INVERSVID);
  202.  
  203.     /* now print a line that says what font and what style it is */
  204.  
  205.     for (j=0; j < afh->afh_NumEntries; j++)
  206.     {
  207.     CStringAppend(&fontname[0],af->af_Attr.ta_Name);  
  208.             /* copy name into build-name area */
  209.             /* already has ".font" onto end of it */
  210.     
  211.     ta.ta_Name = &fontname[0];
  212.     ta.ta_YSize = af->af_Attr.ta_YSize;    /* ask for this size */
  213.     ta.ta_Style = af->af_Attr.ta_Style;    /* ask for designed style */
  214.     ta.ta_Flags = FPF_ROMFONT|FPF_DISKFONT|FPF_PROPORTIONAL|FPF_DESIGNED;
  215.         /* accept it from anywhere it exists */
  216.     style = ta.ta_Style;
  217.  
  218.     if(!((af->af_Attr.ta_Flags & FPF_REMOVED) ||
  219.        (af->af_Attr.ta_Flags & FPF_REVPATH) ||
  220.            ((af->af_Type&AFF_MEMORY)&&
  221.        (af->af_Attr.ta_Flags&FPF_DISKFONT))))
  222.  
  223.        /* this is an IF-NOT, the reverse of the earlier if-test on
  224.         * these same parameters 
  225.         */
  226.            {
  227.         tf = (struct TextFont *) OpenDiskFont(&ta);
  228.     
  229.             if (tf != 0) 
  230.             { 
  231.                 SetFont(w->RPort, tf);
  232.             for(k=0; k<7; k++)
  233.             {
  234.             style = text_styles[k];
  235.                     SetSoftStyle(w->RPort,style,255);
  236.             SetRast(rp,0);    /* erase any previous text */
  237.             Move(rp,10,20);    /* move down a bit from the top */
  238.             sEnd = CStringAppend(&outst[0],af->af_Attr.ta_Name);
  239.             sEnd = sEnd + CStringAppend(&outst[sEnd],"  ");
  240.             sEnd = sEnd + CStringAppend(&outst[sEnd],
  241.                          pointsize[af->af_Attr.ta_YSize]);
  242.             sEnd = sEnd + CStringAppend(&outst[sEnd]," Points, ");
  243.             CStringAppend(&outst[sEnd],text[k]);
  244.             Text(rp,&outst[0],CStringAppend(&dummy[0],&outst[0]));
  245.  
  246.             /* Have to build the string before sending it out to
  247.              * text IF ALGORITHMICALLY GENERATING THE STYLE since 
  248.              * the kerning and spacing tables are based on the
  249.              * vanilla text, and not the algorithmically generated
  250.              * style.  If you send characters out individually,
  251.              * it is possible that the enclosing rectangle of
  252.               * a later character will chop off the trailing edge
  253.              * of a preceding character 
  254.              */
  255.  
  256.             /* ************************************************** 
  257.             This alternate method, when in INVERSVID, exhibits the
  258.             problem described above.
  259.  
  260.             Text(rp,af->af_Attr.ta_Name,STRLEN(af->af_Attr.ta_Name));
  261.             Text(rp,"  ",2);
  262.             Text(rp,pointsize[af->af_Attr.ta_YSize],2);
  263.             Text(rp," Points, ",9);
  264.     
  265.             Text(rp,text[k],textlength[k]);  
  266.             **************************************************  */ 
  267.  
  268.             Delay(40);    /* use the DOS time delay function 
  269.                    * specifies 60ths of a second */
  270.             }
  271.                 CloseFont(tf); /* close the old one */
  272.     
  273.        /* NOTE: 
  274.     *     Even though you close a font, it doesn't get unloaded
  275.     *     Memory unless a font with a different name is specified
  276.     *     for loading.  In this case, any font (except the topaz
  277.     *     set) which has been closed can have its memory area
  278.     *     freed and it will no longer be accessible.  If you close
  279.     *     a font to go to a different point-size, it will NOT cause
  280.     *     a disk-access.  
  281.     *
  282.     *  ALSO NOTE:   
  283.     *      Loading a font loads ALL of the point
  284.     *      sizes contained in that font's directory!!!!
  285.     */
  286.  
  287.             }    /* end of if-tf-ne-0 */
  288.       }    /* end of if-(in memory but from disk) */
  289.     af++;
  290.     }    /* Do next font now */
  291.      }        /* end of for-loop, controlled by m */
  292.  
  293.     FreeMem(afh,AFTABLESIZE);
  294.     CloseWindow(w);
  295.     CloseLibrary(IntuitionBase);   
  296.     CloseLibrary(DosBase);  
  297.     CloseLibrary(DiskfontBase);   
  298.     CloseLibrary(GfxBase);   
  299.  
  300. }
  301.  
  302. /* copy a string and return the number of characters added to 
  303.  * a string.  Effectively returns the length of the string if
  304.  * not adding anything */
  305.  
  306. int CStringAppend(dest, source)    
  307. char *dest;
  308. char *source;
  309. {
  310.     int i=0; 
  311.     char *s = source; 
  312.     char *d = dest; 
  313.     while (( i <79 )&&( *d = *s )) { d++; s++; i++; } 
  314.     /* if find a NULL in source, end the copy, but the NULL itself
  315.      * gets copied over to the destination.  If no NULL, then 79
  316.      * characters get copied, then a terminating NULL is added */
  317.     if(i < 79) return(i);
  318.     else {*d = 0; return(i);    }
  319.     /* value returned is the position of the terminating NULL
  320.      * to allow other strings to be appended simply using the
  321.      * next append command in sequence */
  322.  
  323. }
  324.  
  325.